/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _CLIENT_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "client.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/

#define BAUDRATE B38400
#define FALSE 0
#define TRUE 1

/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/

void *A(void *thread_argv);
void *B(void *thread_argv);


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/

int 	sockfd;
int 	len;
struct sockaddr_in address;
int 	result;

int status;
FILE *input;
FILE *output;

int tty, c, i, error;
char Key;
struct termios oldkey, newkey;       //place tor old and new port settings for keyboard teletype
char buf[60];                       //buffer for where data is put

pthread_mutex_t mutexVAR = PTHREAD_MUTEX_INITIALIZER;
pthread_t threadA, threadB;

/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
main()
{

/* clear socket and tty */
	tcsetattr(tty,TCSANOW,&oldkey);
	close(tty);

	input = fopen("/dev/tty", "r");      //open the terminal keyboard
//	output = fopen("/dev/tty", "w");     //open the terminal screen

	tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK); //set the user console port up
	tcgetattr(tty,&oldkey); // save current port settings   //so commands are interpreted right for this program
	// set new port settings for non-canonical input processing  //must be NOCTTY
	newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	newkey.c_iflag = IGNPAR;
	newkey.c_oflag = 0;
	newkey.c_lflag = 0;       //ICANON;
	newkey.c_cc[VMIN] = 1;
	newkey.c_cc[VTIME] = 0;
	tcflush(tty, TCIFLUSH);
	tcsetattr(tty,TCSANOW,&newkey);

	/* Create a socket for the client. */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Socket name, as agreed with the server. */
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = inet_addr("192.168.1.245");
	address.sin_port = 14;
	len = sizeof(address);

	/* Connect our socket to the server's socket. */
	result = connect(sockfd, (struct sockaddr *)&address, len);
	if(result == -1)
	{
		perror("OOPS!!: connect fnc. error\r\n");
		exit(1);
	}
	printf("connect function ok\r\n");

	pthread_create(&threadA, NULL, &A, NULL);
	pthread_create(&threadB, NULL, &B, NULL);

	pthread_exit(NULL);
	return 0;

}


void *A(void *thread_argv)
{
	while(1)
	{
		// loop while waiting for input. normally we would do something useful here
		status = fread(&Key,1,1,input);
		if (status==1)  //if a key was hit
		{
			if(Key=='1'|Key=='2'|Key=='3'|Key=='4'|Key=='5'|Key=='6'|Key=='7'|Key=='8'|Key=='s'|Key=='d')
			{
				write(sockfd,&Key,1);          //write 1 byte to the port
			}
		}
		else if(Key==0x1b) // Esc
		{
			close(sockfd);
			printf("Close program\r\n");
		}
	}
}

void *B(void *thread_argv)
{
	while(1)
	{
		clr(buf,60);
		result = read(sockfd,buf,60);
		if (result>0)
		{
			printf("%s\r", buf);
		}
	}
}



/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/

